/***************************************************************************
 *   This is a part of the NextCAD Web APP.                                *
 *   Copyright (C) AMCAX, All rights reserved.                             *
 ***************************************************************************/

// import { Alert, Button } from 'antd';
import i18n from 'i18next';

import { CommandDialog } from '@amcax/base';
import { isShapePlaneCopyChild } from '@app-cad/common/utils/plane-copy';
import { CommandLabeledItemWrapper } from '@app-cad/common/components/command-form/item-wrapper';
import { CurvedBaseCommand, CurvedCmdDialogParams } from '@app-cad/types';
import { Select } from 'antd';
import { useState, useEffect } from 'react';
import { CommandCurveIntersection } from './intersection';
import { CommandVertexPlaneInt } from './point-plane-int';
import { CommandSurfaceIntersection } from './surface-intersection';

/**
 *
 * 这里已经被 cross2 替代，保留以兼容处理老数据
 */
function CrossDialog({ title, onClose, props }: CurvedCmdDialogParams) {
  const dxid = props?.shape;
  const isPlaneCopyChild = isShapePlaneCopyChild(dxid);

  const [mode, setMode] = useState<'curve-int' | 'plane-int' | 'Intersection'>(
    'curve-int',
  );

  const [editProps, setEditProps] = useState<any>();
  const [editDxid, setEditDxid] = useState<string>();

  useEffect(() => {
    const mode = props?.params?.type ? 'Intersection' : props?.params?.mode;
    setMode(mode ?? 'curve-int');
    setEditProps(props);
    setEditDxid(dxid);
  }, [dxid]);

  return (
    <CommandDialog
      title={title}
      onClose={onClose}
      placement='topLeft'
    >
      <CommandLabeledItemWrapper
        className='mt-5 px-5'
        label='相交类型'
      >
        <Select
          options={[
            { value: 'curve-int', label: '线线相交' },
            { value: 'plane-int', label: '线面相交' },
            { value: 'Intersection', label: '面面相交' },
          ]}
          value={mode}
          onChange={(val) => {
            if (val !== mode) {
              setMode(val);
              setEditProps(null);
            }
          }}
          disabled={isPlaneCopyChild}
        />
      </CommandLabeledItemWrapper>
      {mode === 'curve-int' && (
        <CommandCurveIntersection
          props={editProps}
          onClose={onClose}
          editDxid={editDxid}
        />
      )}
      {mode === 'plane-int' && (
        <CommandVertexPlaneInt
          props={editProps}
          onClose={onClose}
          editDxid={editDxid}
        />
      )}
      {mode === 'Intersection' && (
        <CommandSurfaceIntersection
          props={editProps}
          onClose={onClose}
          editDxid={editDxid}
          readonly={isPlaneCopyChild}
        />
      )}
    </CommandDialog>
  );
}

export const cmdCross: CurvedBaseCommand = {
  id: 'CmdCross',
  label: 'command.cross',
  icon: 'commands/curved/vertex-plane-int',
  help: {
    remark: 'tips.redo',
  },
  create: (label, onClose, props) => (
    <CrossDialog
      title={i18n.t(label)}
      onClose={onClose}
      props={props}
    />
  ),
};
